#!/usr/bin/env python
import effess
import os
import sys
import inspect
import urllib
import tarfile
import os.path as path
import distutils.dir_util as dirutil
from progressbar import ProgressBar
Import('build')

revisions = {
	"linux-i386": 43,
	"linux-x86_64": 32,
	"osx-i386": 8,
	"win32-i386": 43
}

thirdparty_name = 'thirdparty-%s-%s-r%s' % (build.os, build.arch, revisions[build.os+'-'+build.arch])
url = 'http://tidesdk.multipart.net/thirdparty/legacy/kroll-%s.tgz' % (thirdparty_name)

build.third_party = path.join(build.cwd(), thirdparty_name)
rtdir = build.runtime_build_dir

def exists():
	return path.isdir(build.third_party)

def fetch():
	print "You don't seem to have the appropriate thirdparty files. I'll fetch them."
	print "Downloading %s" % url

	pbar = ProgressBar().start()
	try:
		def progress_callback(count, block_size, total_size):
			if (total_size < 0):
				raise Exception("Could not fetch archive! Does it exist on the server?")
			percent = int(count * block_size * 100/total_size)
			pbar.update(percent)

		fname, msg = urllib.urlretrieve(url, reporthook=progress_callback)

		print "Fetched it (%s). I'm going to unpack it now..." % (fname)
		os.makedirs(build.third_party)
		tfile = tarfile.open(fname, mode="r:gz")
		tfile.extractall(path=build.third_party)
	finally:
		urllib.urlcleanup()
		pbar.finish()

if not exists(): fetch()

if build.is_linux():
	effess.copy_tree(path.join(build.third_party, 'webkit', 'lib'), rtdir)
	effess.copy_tree(path.join(build.third_party, 'poco', 'lib'), rtdir)

elif build.is_win32():
	effess.copy_tree(path.join(build.third_party, 'poco', 'bin'), rtdir)
	effess.copy_tree(path.join(build.third_party, 'webkit', 'bin'), rtdir)
	effess.copy_tree(path.join(build.third_party, 'sdk'), path.join(build.dir, 'sdk'))

elif build.is_osx():
	excludes = ['.h', '.defs', 'JavaScriptGlue.framework']
	targets = []
	for framework in Glob(path.join(build.third_party, '*/*.framework')):
		t = build.utils.CopyToDir(framework, rtdir, exclude=excludes)
		targets.append(t)

	# PHP dependencies don't aren't distributed with the runtime, only Poco's.
	# for libdir in Glob(path.join(build.third_party, '*/lib')):
	libdir = path.join(build.third_party, "poco", "lib");
	targets.append(build.utils.CopyTree(libdir, rtdir, exclude=excludes))
	build.mark_build_target(targets)

